package com.medicalsystem.service;

import com.medicalsystem.entity.Diagnosis;
import com.medicalsystem.repository.DiagnosisRepository;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Optional;

@Service
public class DiagnosisService {

    private final DiagnosisRepository diagnosisRepository;

    public DiagnosisService(DiagnosisRepository diagnosisRepository) {
        this.diagnosisRepository = diagnosisRepository;
    }

    // 获取所有诊断记录
    public List<Diagnosis> getAllDiagnoses() {
        return diagnosisRepository.findAll();
    }

    // 根据ID获取诊断记录
    public Diagnosis getDiagnosisById(Long id) {
        return diagnosisRepository.findById(id).orElse(null);
    }

    // 新增诊断记录
    public Diagnosis saveDiagnosis(Diagnosis diagnosis) {
        diagnosis.setCreateTime(new java.util.Date());
        diagnosis.setUpdateTime(new java.util.Date());
        return diagnosisRepository.save(diagnosis);
    }

    // 修改诊断记录
    public Diagnosis updateDiagnosis(Long id, Diagnosis updatedDiagnosis) {
        Optional<Diagnosis> existingDiagnosis = diagnosisRepository.findById(id);
        if (existingDiagnosis.isPresent()) {
            Diagnosis diagnosis = existingDiagnosis.get();
            diagnosis.setAppointmentId(updatedDiagnosis.getAppointmentId());
            diagnosis.setPatientId(updatedDiagnosis.getPatientId());
            diagnosis.setPatientName(updatedDiagnosis.getPatientName());
            diagnosis.setDoctorId(updatedDiagnosis.getDoctorId());
            diagnosis.setDoctorName(updatedDiagnosis.getDoctorName());
            diagnosis.setDepartment(updatedDiagnosis.getDepartment());
            diagnosis.setDiagnosisDate(updatedDiagnosis.getDiagnosisDate());
            diagnosis.setMainComplaint(updatedDiagnosis.getMainComplaint());
            diagnosis.setSymptoms(updatedDiagnosis.getSymptoms());
            diagnosis.setDiagnosis(updatedDiagnosis.getDiagnosis());
            diagnosis.setTreatment(updatedDiagnosis.getTreatment());
            diagnosis.setMedication(updatedDiagnosis.getMedication());
            diagnosis.setStatus(updatedDiagnosis.getStatus());
            diagnosis.setUpdateTime(new java.util.Date());
            return diagnosisRepository.save(diagnosis);
        }
        return null;
    }

    // 根据ID删除诊断记录
    public void deleteDiagnosis(Long id) {
        diagnosisRepository.deleteById(id);
    }
}
